package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.GDTabbedSearchDialog;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class DamageFullSelectionPane extends AdjustablePanel {
    private WeaponTypePane pnlWeaponTypes;
    private ArmorTypePane pnlArmorTypes;
    private MiscTypePane pnlMiscTypes;
    private ItemRarityPane pnlItemRarity;
    private StatReqPane pnlStats;
    private DamageSelectionPane pnlDamage;
    private TitledBorder brdItem;
    private GDTabbedSearchDialog.Mode mode;

    public DamageFullSelectionPane(final GDTabbedSearchDialog.Mode mode) {
        this.mode = mode;
        this.adjustUI();
        final JPanel pnlItemTypes = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(pnlItemTypes);
        pnlItemTypes.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlWeaponTypes)).addGroup(layout.createParallelGroup().addComponent(this.pnlArmorTypes)).addGroup(layout.createParallelGroup().addComponent(this.pnlMiscTypes));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlWeaponTypes).addComponent(this.pnlArmorTypes).addComponent(this.pnlMiscTypes));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlWeaponTypes, this.pnlArmorTypes);
        layout.linkSize(0, this.pnlWeaponTypes, this.pnlMiscTypes);
        layout.linkSize(1, this.pnlWeaponTypes, this.pnlArmorTypes);
        layout.linkSize(1, this.pnlWeaponTypes, this.pnlMiscTypes);
        final JPanel pnlItemInfo = new JPanel();
        layout = new GroupLayout(pnlItemInfo);
        pnlItemInfo.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlItemTypes).addComponent(this.pnlItemRarity).addComponent(this.pnlStats));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlItemTypes)).addGroup(layout.createParallelGroup().addComponent(this.pnlItemRarity)).addGroup(layout.createParallelGroup().addComponent(this.pnlStats));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, pnlItemTypes, this.pnlItemRarity);
        layout.linkSize(0, pnlItemTypes, this.pnlStats);
        pnlItemInfo.setBorder(this.brdItem);
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlItemInfo)).addGroup(layout.createParallelGroup().addComponent(this.pnlDamage));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlItemInfo).addComponent(this.pnlDamage));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(1, pnlItemInfo, this.pnlDamage);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        if (this.brdItem == null) {
            this.brdItem = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_ITEM_INFO"));
        }
        this.brdItem.setTitleFont(fntBorder);
        if (this.pnlWeaponTypes == null) {
            this.pnlWeaponTypes = new WeaponTypePane();
        } else {
            this.pnlWeaponTypes.adjustUI();
        }
        if (this.pnlArmorTypes == null) {
            this.pnlArmorTypes = new ArmorTypePane();
        } else {
            this.pnlArmorTypes.adjustUI();
        }
        if (this.pnlMiscTypes == null) {
            this.pnlMiscTypes = new MiscTypePane(this.mode);
        } else {
            this.pnlMiscTypes.adjustUI();
        }
        if (this.pnlItemRarity == null) {
            this.pnlItemRarity = new ItemRarityPane(0);
        } else {
            this.pnlItemRarity.adjustUI();
        }
        if (this.pnlStats == null) {
            this.pnlStats = new StatReqPane();
        } else {
            this.pnlStats.adjustUI();
        }
        if (this.pnlDamage == null) {
            this.pnlDamage = new DamageSelectionPane();
        } else {
            this.pnlDamage.adjustUI();
        }
    }

    public void clear() {
        this.pnlWeaponTypes.clear();
        this.pnlArmorTypes.clear();
        this.pnlMiscTypes.clear();
        this.pnlItemRarity.clear();
        this.pnlStats.clear();
        this.pnlDamage.clear();
    }

    public void addCriteria(final SelectionCriteria criteria) {
        this.pnlWeaponTypes.addCriteria(criteria);
        this.pnlArmorTypes.addCriteria(criteria);
        this.pnlMiscTypes.addCriteria(criteria);
        this.pnlItemRarity.addCriteria(criteria);
        this.pnlStats.addCriteria(criteria);
        this.pnlDamage.addCriteria(criteria);
        criteria.selMode = SelectionCriteria.SelectionMode.ITEM;
    }
}
